package caiziyou.com.designpattern.principle.inversion.improve;

public class DependencyPass {

	public static void main(String[] args) {
		//1
		ChangHong changHong = new ChangHong();
		OpenAndClose openAndClose = new OpenAndClose();
		openAndClose.open(changHong);
		
		//2
//		ChangHong changHong = new ChangHong();
//		OpenAndClose openAndClose = new OpenAndClose(changHong);
//		openAndClose.open();
		
		//3
//		ChangHong changHong = new ChangHong();
//		OpenAndClose openAndClose = new OpenAndClose();
//		openAndClose.setTv(changHong);
//		openAndClose.open();
	}

}

class ChangHong implements ITV{

	@Override
	public void play() {
		System.out.println("长虹电视机打开了");
	}
	
}

//1 第一种方式通过接口传递，实现依赖
/**
 * 开放开关接口
 * 
 * @author Administrator
 *
 */
interface IOpenAndClose {
	public void open(ITV tv);
}

/**
 * ITV 接口
 * 
 * @author Administrator
 *
 */
interface ITV {
	public void play();
}

/**
 * 实现接口
 * 
 * @author Administrator
 *
 */
class OpenAndClose implements IOpenAndClose {
	@Override
	public void open(ITV tv) {
		tv.play();
	}

}

//方式二 通过构造器实现依赖

//interface IOpenAndClose {
//	public void open();
//}
//
//interface ITV {
//	public void play();
//}
//
//class OpenAndClose implements IOpenAndClose {
//	private ITV tv;
//
//	public OpenAndClose(ITV tv) {
//		super();
//		this.tv = tv;
//	}
//
//	@Override
//	public void open() {
//		this.tv.play();
//	}
//
//}

//方式3 通过setter方法实现依赖
//interface IOpenAndClose {
//	public void open();
//
//	public void setTv(ITV tv);
//}
//
//interface ITV {
//	public void play();
//}
//
//class OpenAndClose implements IOpenAndClose {
//	private ITV tv;
//
//	@Override
//	public void setTv(ITV tv) {
//		this.tv = tv;
//	}
//
//	@Override
//	public void open() {
//		this.tv.play();
//	}
//
//}